var diameterOfBinaryTree = function (root) {
    let maxLength = 0

    function dfs(node) {
        if (node === null) return 0;
        let leftDepth = dfs(node.left)
        let rightDepth = dfs(node.right)
        maxLength = Math.max(maxLength, leftDepth + rightDepth)
        return Math.max(leftDepth, rightDepth) + 1
    }
    dfs(root)
    return maxLength;
};